############################################################################################################################################
# The Cultural Origins of Populism ----------------------------
# Yotam Margalit, Shir Raviv and Omer Solodoch  ---------------------------
# This R file contains the code necessary to create the data for the main analysis ----------------------------
###########################################################################################
# Clear environment #############

rm(list = ls())
gc()


# Installing and Loading Packages #############

#install.packages("dplyr")
#install.packages("stats")
#install.packages("base")
#install.packages("factor")
#install.packages("tibble")
library(dplyr)
library(stats)
library(base)
library(questionr)
library(labelled)
library(rio)
library(tibble)
library(base)

# Set Working Directory  ############
setwd("insert here.. /JOP Replication")
plotpath<-"figures/"

#### Data ---------
ess_data <- readRDS("data/ESS_69_raw_data.rds")

## Generate key variables -----
# Wave -----
ess_data$Wave<-factor(ess_data$essround, levels = c(6,7,8,9),
                      labels = c("2012", "2014", "2016", "2018"))

# cntry2 -----
ess_data$cntry2<-factor(ess_data$cntry)
ess_data$cntry<-factor(ess_data$cntry2, levels = c("CH", "DE", "DK", "FR", "GB",
                                                   "HU", "IT","NL", "PL", "SE"),
                       labels = c("Switzerland","Germany", "Denmark","France",
                                  "United Kingdom","Hungary",
                                  "Italy","Netherlands",
                                  "Poland","Sweden"))

# Weights: post-stratification * population size -----
ess_data$fullweight = ess_data$pspwght * ess_data$pweight

# Occupation  -----
# Recode occupation variable ISCO08: Oesch (2006) class position

ess_data$isco_mainjob <- ess_data$isco08
ess_data$isco_mainjob[is.na(ess_data$isco_mainjob)] <- -9
var_label(ess_data$isco_mainjob) <- "Current occupation of respondent - isco08 4-digit"



#### Recode employment status for respondents
ess_data$emplrel_r <- ess_data$emplrel
ess_data$emplrel_r[is.na(ess_data$emplrel_r)] <- 9
val_label(ess_data$emplrel_r, 9) <- "Missing"


ess_data$emplno_r <- ess_data$emplno
ess_data$emplno_r[is.na(ess_data$emplno_r)] <- 0
ess_data$emplno_r[ess_data$emplno_r >= 1 & ess_data$emplno_r <= 9] <- 1
ess_data$emplno_r[ess_data$emplno_r >= 10 & ess_data$emplno_r <= 66665] <- 2
val_labels(ess_data$emplno_r) <- c("0 employees" = 0,
                                   "1-9 employees" = 1,
                                   "10+ employees" = 2)
freq(ess_data$emplno_r, total = T)

ess_data$selfem_mainjob <- NA
ess_data$selfem_mainjob[ess_data$emplrel_r == 1 | ess_data$emplrel_r == 9] <- 1
ess_data$selfem_mainjob[ess_data$emplrel_r == 2 & ess_data$emplno_r == 0] <- 2
ess_data$selfem_mainjob[ess_data$emplrel_r == 3] <- 2
ess_data$selfem_mainjob[ess_data$emplrel_r == 2 & ess_data$emplno_r == 1] <- 3
ess_data$selfem_mainjob[ess_data$emplrel_r == 2 & ess_data$emplno_r == 2] <- 4

val_labels(ess_data$selfem_mainjob) <- c("Not self-employed" = 1,
                                         "Self-empl without employees" = 2,
                                         "Self-empl with 1-9 employees" = 3,
                                         "Self-empl with 10 or more" = 4)
var_label(ess_data$selfem_mainjob) <- "Employment status for respondants"
freq(ess_data$selfem_mainjob, total = T)


ess_data$class16_r <- -9

# Large employers (1)

ess_data$class16_r[ess_data$selfem_mainjob == 4] <- 1

# Self-employed professionals (2)

ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2000 & ess_data$isco_mainjob <= 2162] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2164 & ess_data$isco_mainjob <= 2165] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2200 & ess_data$isco_mainjob <= 2212] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob == 2250] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2261 & ess_data$isco_mainjob <= 2262] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2300 & ess_data$isco_mainjob <= 2330] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2350 & ess_data$isco_mainjob <= 2352] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2359 & ess_data$isco_mainjob <= 2432] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2500 & ess_data$isco_mainjob <= 2619] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob == 2621] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2630 & ess_data$isco_mainjob <= 2634] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2636 & ess_data$isco_mainjob <= 2640] <- 2
ess_data$class16_r[(ess_data$selfem_mainjob == 2 | ess_data$selfem_mainjob == 3) & ess_data$isco_mainjob >= 2642 & ess_data$isco_mainjob <= 2643] <- 2

# Small business owners with employees (3)

ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 1000 & ess_data$isco_mainjob <= 1439] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2163] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2166] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2220 & ess_data$isco_mainjob <= 2240] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2260] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2263 & ess_data$isco_mainjob <= 2269] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2340 & ess_data$isco_mainjob <= 2342] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2353 & ess_data$isco_mainjob <= 2356] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2433 & ess_data$isco_mainjob <= 2434] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2620] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2622] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2635] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob == 2641] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 2650 & ess_data$isco_mainjob <= 2659] <- 3
ess_data$class16_r[ess_data$selfem_mainjob == 3 & ess_data$isco_mainjob >= 3000 & ess_data$isco_mainjob <= 9629] <- 3

# Small business owners without employees (4)

ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 1000 & ess_data$isco_mainjob <= 1439] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2163] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2166] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2220 & ess_data$isco_mainjob <= 2240] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2260] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2263 & ess_data$isco_mainjob <= 2269] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2340 & ess_data$isco_mainjob <= 2342] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2353 & ess_data$isco_mainjob <= 2356] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2433 & ess_data$isco_mainjob <= 2434] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2620] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2622] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2635] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob == 2641] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 2650 & ess_data$isco_mainjob <= 2659] <- 4
ess_data$class16_r[ess_data$selfem_mainjob == 2 & ess_data$isco_mainjob >= 3000 & ess_data$isco_mainjob <= 9629] <- 4

# Technical experts (5)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2100 &  ess_data$isco_mainjob <= 2162] <- 5
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2164 &  ess_data$isco_mainjob <= 2165] <- 5
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2500 &  ess_data$isco_mainjob <= 2529] <- 5

# Technicians (6)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3100 &  ess_data$isco_mainjob <= 3155] <- 6
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3210 &  ess_data$isco_mainjob <= 3214] <- 6
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3252] <- 6
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3500 &  ess_data$isco_mainjob <= 3522] <- 6

# Skilled manual (7)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 6000 &  ess_data$isco_mainjob <= 7549] <- 7
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 8310 &  ess_data$isco_mainjob <= 8312] <- 7
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 8330] <- 7
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 8332 &  ess_data$isco_mainjob <= 8340] <- 7
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 8342 &  ess_data$isco_mainjob <= 8344] <- 7

# Low-skilled manual (8)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 8000 &  ess_data$isco_mainjob <= 8300] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 8320 &  ess_data$isco_mainjob <= 8321] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 8341] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 8350] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 9200 &  ess_data$isco_mainjob <= 9334] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 9600 &  ess_data$isco_mainjob <= 9620] <- 8
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 9622 &  ess_data$isco_mainjob <= 9629] <- 8

# Higher-grade managers and administrators (9)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 1000 &  ess_data$isco_mainjob <= 1300] <- 9
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 1320 &  ess_data$isco_mainjob <= 1349] <- 9
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2400 &  ess_data$isco_mainjob <= 2432] <- 9
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2610 &  ess_data$isco_mainjob <= 2619] <- 9
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2631] <- 9
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 100 &  ess_data$isco_mainjob <= 110] <- 9

# Lower-grade managers and administrators (10)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 1310 &  ess_data$isco_mainjob <= 1312] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 1400 &  ess_data$isco_mainjob <= 1439] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2433 &  ess_data$isco_mainjob <= 2434] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3300 &  ess_data$isco_mainjob <= 3339] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3343] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3350 &  ess_data$isco_mainjob <= 3359] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3411] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5221] <- 10
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 200 &  ess_data$isco_mainjob <= 210] <- 10

# Skilled clerks (11)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3340 &  ess_data$isco_mainjob <= 3342] <- 11
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3344] <- 11
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 4000 &  ess_data$isco_mainjob <= 4131] <- 11
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 4200 &  ess_data$isco_mainjob <= 4221] <- 11
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 4224 &  ess_data$isco_mainjob <= 4413] <- 11
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 4415 &  ess_data$isco_mainjob <= 4419] <- 11

# Unskilled clerks (12)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 4132] <- 12
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 4222] <- 12
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 4223] <- 12
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5230] <- 12
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 9621] <- 12

# Socio-cultural professionals (13)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2200 &  ess_data$isco_mainjob <= 2212] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2250] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2261 &  ess_data$isco_mainjob <= 2262] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2300 &  ess_data$isco_mainjob <= 2330] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2350 &  ess_data$isco_mainjob <= 2352] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2359] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2600] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2621] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2630] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2632 &  ess_data$isco_mainjob <= 2634] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2636 &  ess_data$isco_mainjob <= 2640] <- 13
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2642 &  ess_data$isco_mainjob <= 2643] <- 13

# Socio-cultural semi-professionals (14)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2163] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2166] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2220 &  ess_data$isco_mainjob <= 2240] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2260] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2263 &  ess_data$isco_mainjob <= 2269] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2340 &  ess_data$isco_mainjob <= 2342] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2353 &  ess_data$isco_mainjob <= 2356] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2620] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2622] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2635] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 2641] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 2650 &  ess_data$isco_mainjob <= 2659] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3200] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3220 &  ess_data$isco_mainjob <= 3230] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3250] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3253 &  ess_data$isco_mainjob <= 3257] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3259] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3400 &  ess_data$isco_mainjob <= 3410] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3412 &  ess_data$isco_mainjob <= 3413] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3430 &  ess_data$isco_mainjob <= 3433] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3435] <- 14
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 4414] <- 14

# Skilled service (15)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3240] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3251] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3258] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 3420 &  ess_data$isco_mainjob <= 3423] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 3434] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5000 &  ess_data$isco_mainjob <= 5120] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5140 &  ess_data$isco_mainjob <= 5142] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5163] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5165] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5200] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5220] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5222 &  ess_data$isco_mainjob <= 5223] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5241 &  ess_data$isco_mainjob <= 5242] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5300 &  ess_data$isco_mainjob <= 5321] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5400 &  ess_data$isco_mainjob <= 5413] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5419] <- 15
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 8331] <- 15

# Low-skilled service (16)

ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5130 &  ess_data$isco_mainjob <= 5132] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5150 &  ess_data$isco_mainjob <= 5162] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5164] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5169] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5210 &  ess_data$isco_mainjob <= 5212] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5240] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5243 &  ess_data$isco_mainjob <= 5249] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 5322 &  ess_data$isco_mainjob <= 5329] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 5414] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob == 8322] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 9100 &  ess_data$isco_mainjob <= 9129] <- 16
ess_data$class16_r[ess_data$selfem_mainjob == 1 & ess_data$isco_mainjob >= 9400 &  ess_data$isco_mainjob <= 9520] <- 16


ess_data$class16_r[ess_data$class16_r == -9] <- NA
val_labels(ess_data$class16_r) <- c("Large employers" = 1,
                                    "Self-employed professionals" = 2,
                                    "Small business owners with employees" = 3,
                                    "Small business owners without employees" = 4,
                                    "Technical experts" = 5,
                                    "Technicians" = 6,
                                    "Skilled manual" = 7,
                                    "Low-skilled manual" = 8,
                                    "Higher-grade managers and administrators" = 9,
                                    "Lower-grade managers and administrators" = 10,
                                    "Skilled clerks" = 11,
                                    "Unskilled clerks" = 12,
                                    "Socio-cultural professionals" = 13,
                                    "Socio-cultural semi-professionals" = 14,
                                    "Skilled service" = 15,
                                    "Low-skilled service" = 16)
var_label(ess_data$class16_r) <- "Respondent's Oesch class position - 16 classes"
freq(ess_data$class16_r, total = T)


ess_data$class8_r <- NA
ess_data$class8_r[ess_data$class16_r <= 2] <- 1
ess_data$class8_r[ess_data$class16_r == 3 | ess_data$class16_r == 4] <- 2
ess_data$class8_r[ess_data$class16_r == 5 | ess_data$class16_r == 6] <- 3
ess_data$class8_r[ess_data$class16_r == 7 | ess_data$class16_r == 8] <- 4
ess_data$class8_r[ess_data$class16_r == 9 | ess_data$class16_r == 10] <- 5
ess_data$class8_r[ess_data$class16_r == 11 | ess_data$class16_r == 12] <- 6
ess_data$class8_r[ess_data$class16_r == 13 | ess_data$class16_r == 14] <- 7
ess_data$class8_r[ess_data$class16_r == 15 | ess_data$class16_r == 16] <- 8
val_labels(ess_data$class8_r) <- c("Self-employed professionals and large employers" = 1,
                                   "Small business owners" = 2,
                                   "Technical (semi-)professionals" = 3,
                                   "Production workers" = 4,
                                   "(Associate) managers" = 5,
                                   "Clerks" = 6,
                                   "Socio-cultural (semi-)professionals" = 7,
                                   "Service workers" = 8)
var_label(ess_data$class8_r) <- "Respondent's Oesch class position - 8 classes"
freq(ess_data$class8_r, total = T)



ess_data$class5_r <- NA
ess_data$class5_r[ess_data$class16_r <= 2 | ess_data$class16_r == 5 | ess_data$class16_r == 9 | ess_data$class16_r == 13] <- 1
ess_data$class5_r[ess_data$class16_r == 6 | ess_data$class16_r == 10 | ess_data$class16_r == 14] <- 2
ess_data$class5_r[ess_data$class16_r == 3 | ess_data$class16_r == 4] <- 3
ess_data$class5_r[ess_data$class16_r == 7 | ess_data$class16_r == 11 | ess_data$class16_r == 15] <- 4
ess_data$class5_r[ess_data$class16_r == 8 | ess_data$class16_r == 12 | ess_data$class16_r == 16] <- 5
val_labels(ess_data$class5_r) <- c("Higher-grade service class" = 1,
                                   "Lower-grade service class" = 2,
                                   "Small business owners" = 3,
                                   "Skilled workers" = 4,
                                   "Unskilled workers" = 5)
var_label(ess_data$class5_r) <- "Respondent's Oesch class position - 5 classes"

########################################################################### 
## Socio-Demographics Indicators ----
###########################################################################  
# Age --------
ess_data$yrbrn[ess_data$yrbrn      > 2003] <- NA 
ess_data$agea[ess_data$agea      > 150] <- NA 

# Older voters 60+
ess_data<-ess_data %>%
  mutate(older65= case_when(agea>64~1,
                            agea<65~0))
# Older voters 55+
ess_data<-ess_data %>%
  mutate(older55= case_when(agea>54~1,
                            agea<55~0))

ess_data$older55[is.na(ess_data$yrbrn)] <- NA

# Create Generation Cohorts (Norris and Ingelhart (2018)'s calssification)  

ess_data$Interwar_cohort<- as.numeric (with(ess_data, ifelse ((yrbrn<1946),1,0)))
ess_data$Interwar_cohort[is.na(ess_data$yrbrn)] <- NA

ess_data$BabyBoomers_cohort<- as.numeric (with(ess_data, ifelse ((yrbrn<1966),1,0)))
ess_data$BabyBoomers_cohort[is.na(ess_data$yrbrn)] <- NA

ess_data$GenX_cohort<- as.numeric (with(ess_data, ifelse ((yrbrn<1980),1,0)))
ess_data$GenX_cohort[is.na(ess_data$yrbrn)] <- NA

ess_data$Millennials_cohort<- as.numeric (with(ess_data, ifelse ((yrbrn>1979),1,0)))
ess_data$Millennials_cohort[is.na(ess_data$yrbrn)] <- NA


# Rural area ------
ess_data$domicil[ess_data$domicil> 5]  <- NA

ess_data<-ess_data %>%
  mutate(Rural_area= case_when( domicil==4 |domicil==5 ~1,
                                domicil==1 | domicil==2 | domicil==3~0))
ess_data$Rural_area[is.na(ess_data$Rural_area)] <- NA

ess_data$Rural_area_fac<-factor(ess_data$Rural_area,
                                levels=c(0,1), labels=c("Urban","Rural"))

# Native ----
# Born in country - Were you born in [country]?  1	Yes  2	No 7 
ess_data<-ess_data %>%
  mutate(born_incountry= case_when(brncntr==1~1,
                                   brncntr==2~0))
ess_data$born_incountry[is.na(ess_data$brncntr)] <- NA
ess_data$born_incountry_fac<-factor(ess_data$born_incountry,
                                    levels=c(0,1), labels=c("Not a native","Native"))


# Father born in country - facntr -  Was your father born in [country]?
ess_data<-ess_data %>%
  mutate(father_born_incountry= case_when(facntr==1 ~1,
                                          facntr==2~0))
ess_data$father_born_incountry[is.na(ess_data$facntr)] <- NA

ess_data$father_born_incountry_fac<-factor(ess_data$father_born_incountry,
                                           levels=c(0,1), labels=c("Father isn't native","Father is native"))

# Religious ----
ess_data$rlgdgr[ess_data$rlgdgr   > 10] <- NA
ess_data$very_relig<- as.numeric (with(ess_data, ifelse ((rlgdgr>7),1,0))) 
ess_data$very_relig[is.na(ess_data$rlgdgr)] <- NA

ess_data$very_relig_fac<-factor(ess_data$very_relig)


# Belong to minority ethnic group in country ----
ess_data$blgetmg[ess_data$blgetmg > 2]  <- NA
ess_data$ethnic_minority<- NA
ess_data$ethnic_minority[ess_data$blgetmg ==1] <- 1
ess_data$ethnic_minority[ess_data$blgetmg ==2] <- 0
ess_data$ethnic_minority[is.na(ess_data$blgetmg)] <- NA

# Ethnic minority & native second gen ----
ess_data$ethnic_minority_Native_secgen<- as.numeric (with(ess_data, ifelse ((born_incountry==1 & 
                                                                               father_born_incountry==1 & 
                                                                               ethnic_minority==0),1,0)))

# Gender ----
ess_data$gndr[ess_data$gndr   > 2]  <- NA
ess_data<-ess_data %>%
  mutate(Male= case_when(gndr==1 ~1,
                         gndr==2~0))
ess_data$Male[is.na(ess_data$gndr)] <- NA

ess_data$Male_fac<-factor(ess_data$Male,
                          levels=c(0,1), labels=c("Female","Male"))

# Low Education ----
ess_data$eduyrs[ess_data$eduyrs   > 70] <- NA

# Low education
ess_data<-ess_data %>%
  mutate(low_education= case_when(eduyrs<12 ~1,
                                  eduyrs>11~0))
ess_data$low_education[is.na(ess_data$eduyrs)] <- NA

ess_data$low_education_fa<-factor(ess_data$low_education,
                                  levels=c(0,1), labels=c("More than 12 years of education",
                                                          "Less than 12 years of education"))

# Social Class (production) ----
# 8-Class schema constructed
#1 Self-employed professionals and large employers
#2 Small business owners
#3 Technical (semi-)professionals
#4 Production workers
#5 (Associate) managers
#6 Clerks
#7 Socio-cultural (semi-)professionals
#8 Service workers
ess_data$production_worker<- as.numeric (with(ess_data, ifelse ((class8_r==4),1,0))) 
ess_data$production_worker[is.na(ess_data$class8_r) ] <- NA

ess_data$Unskilled_workers<- as.numeric (with(ess_data, ifelse ((class8_r==4 & low_education==1),1,0)))
ess_data$Unskilled_workers[is.na(ess_data$class8_r)
                           | is.na(ess_data$low_education) ] <- NA


# Unemployed --------------------
ess_data$uempla[ess_data$uempla   > 2]  <- NA
ess_data<-ess_data %>%
  mutate(unemployed_lookingforjob= case_when(uempla==1 ~1,
                                             uempla==0~0))
ess_data$unemployed_lookingforjob[is.na(ess_data$uempla)] <- NA

ess_data$unemployed_lookingforjob_fac<-factor(ess_data$unemployed_lookingforjob,
                                              levels=c(0,1), labels=c("other","unemployed"))

# Low Income --------------------
# Consolidate income to 10-point scale
ess_data <- ess_data %>%
  mutate(income = case_when(
    hinctnt  %in% c(11:12) ~ 10,
    hinctnt  %in% c(1:10)  ~ as.numeric(hinctnt),
    hinctnta %in% c(1:10)  ~ as.numeric(hinctnta) ))
median(ess_data$income, na.rm = TRUE)

ess_data<-ess_data %>%
  group_by(cntry, Wave) %>%
  mutate(median_income=median(income, na.rm = TRUE))
ess_data<-ess_data %>%
  mutate(median_income_2=median_income*2/3) 
ess_data<-ess_data %>%
  mutate(below_2median_income=case_when(income<median_income_2~1,
                                        income>=median_income_2~0))
ess_data$below_2median_income[is.na(ess_data$median_income_2)] <- NA

# Subjective economic insecurity --------------------
ess_data$hincfel[ess_data$hincfel > 4]  <- NA 
ess_data$difficult_subj<- as.numeric (with(ess_data, ifelse ((hincfel>2),1,0)))
ess_data$difficult_subj[is.na(ess_data$hincfel)] <- NA

ess_data$difficultecon_subj_fac<-factor(ess_data$difficult_subj,
                                        levels=c(0,1), labels=c("Comfortably","Difficult"))
# White native working class ------
ess_data$white_workingclass<- as.numeric (with(ess_data, ifelse ((low_education==1 &
                                                                    class8_r==8 & 
                                                                    ethnic_minority==0  &
                                                                    born_incountry==1),1,0)))
ess_data$white_workingclass[is.na(ess_data$low_education) |
                              is.na(ess_data$class8_r) |
                              is.na(ess_data$ethnic_minority) |
                              is.na(ess_data$born_incountry) ] <- NA

# Economic insecurity ------
ess_data$econ_inse_obj<- as.numeric (with(ess_data, ifelse (((class8_r==4 & 
                                                                low_education==1) | 
                                                               unemployed_lookingforjob==1),1,0)))
ess_data$econ_inse_obj[is.na(ess_data$class8_r)  |
                         is.na(ess_data$low_education)  |
                         is.na(ess_data$unemployed_lookingforjob)   ] <- NA

# Foreign-born population ------
ess_data<-ess_data %>%
  mutate(foreignborn_share= case_when(cntry=="Switzerland" & Wave=="2012"~ 27,
                                      cntry=="Switzerland" & Wave=="2014"~ 27.9,
                                      cntry=="Switzerland" & Wave=="2016"~ 28.8,
                                      cntry=="Switzerland" & Wave=="2018"~ 29.5,
                                      cntry=="Germany" & Wave=="2012"~ 12.1,
                                      cntry=="Germany" & Wave=="2014"~ 12.8,
                                      cntry=="Germany" & Wave=="2016"~ 13.9,
                                      cntry=="Germany" & Wave=="2018"~ 15.8,
                                      cntry=="Denmark" & Wave=="2012"~ 7.9,
                                      cntry=="Denmark" & Wave=="2014"~ 8.4,
                                      cntry=="Denmark" & Wave=="2016"~ 9.5,
                                      cntry=="Denmark" & Wave=="2018"~ 10.3,
                                      cntry=="France" & Wave=="2012"~ 11.8,
                                      cntry=="France" & Wave=="2014"~ 12,
                                      cntry=="France" & Wave=="2016"~ 12.3,
                                      cntry=="France" & Wave=="2018"~ 12.5,
                                      cntry=="United Kingdom" & Wave=="2012"~ 11.8,
                                      cntry=="United Kingdom" & Wave=="2014"~ 12.3,
                                      cntry=="United Kingdom" & Wave=="2016"~ 13.6,
                                      cntry=="United Kingdom" & Wave=="2018"~ 13.7,
                                      cntry=="Hungary" & Wave=="2012"~ 4.1,
                                      cntry=="Hungary" & Wave=="2014"~ 4.6,
                                      cntry=="Hungary" & Wave=="2016"~ 5.2,
                                      cntry=="Hungary" & Wave=="2018"~ 5.5,
                                      cntry=="Italy" & Wave=="2012"~ 9.5,
                                      cntry=="Italy" & Wave=="2014"~ 9.5,
                                      cntry=="Italy" & Wave=="2016"~ 9.7,
                                      cntry=="Italy" & Wave=="2018"~ 10.2,
                                      cntry=="Netherlands" & Wave=="2012"~ 11.4,
                                      cntry=="Netherlands" & Wave=="2014"~ 11.6,
                                      cntry=="Netherlands" & Wave=="2016"~ 12.1,
                                      cntry=="Netherlands" & Wave=="2018"~ 13,
                                      cntry=="Poland" & Wave=="2012"~ 1.6,
                                      cntry=="Poland" & Wave=="2014"~ 1.6,
                                      cntry=="Poland" & Wave=="2016"~ 1.6,
                                      cntry=="Poland" & Wave=="2018"~ 1.8,
                                      cntry=="Sweden" & Wave=="2012"~ 15,
                                      cntry=="Sweden" & Wave=="2014"~ 15.8,
                                      cntry=="Sweden" & Wave=="2016"~ 17,
                                      cntry=="Sweden" & Wave=="2018"~ 18.8,))


ess_data<-ess_data %>%
  mutate(per30_immigrants= foreignborn_share*3)

ess_data<-ess_data %>%
  mutate(per20_immigrants= foreignborn_share*2)

########################################################################### 
## Attitudinal Indicators ----
###########################################################################  
# Country's cultural life undermined by immigrants ------
ess_data$imueclt[ess_data$imueclt > 10] <- NA 
ess_data<-ess_data %>%
  mutate(img_undermined_cult= case_when(imueclt<4 ~1,
                                        imueclt>3~0))
ess_data$img_undermined_cult[is.na(ess_data$imueclt)  ] <- NA

ess_data$img_undermined_cult_fac<-factor(ess_data$img_undermined_cult)

# Of every 100 people in country how many born outside country ------
ess_data$noimbro[ess_data$noimbro > 100] <- NA 
ess_data$morethan30per_immigrants<- as.numeric (with(ess_data, 
                                                     ifelse ((noimbro>per30_immigrants),1,0)))

ess_data$morethan30per_immigrants[is.na(ess_data$noimbro)  ] <- NA

ess_data$morethan20per_immigrants<- as.numeric (with(ess_data, 
                                                     ifelse ((noimbro>per20_immigrants),1,0)))

ess_data$morethan20per_immigrants[is.na(ess_data$noimbro)  ] <- NA

# How would you describe the area where you currently live?  ------
ess_data$acetalv[ess_data$acetalv > 3] <- NA 
#1 Almost nobody minority race/ethnic group
#2          Some minority race/ethnic group
#3          Many minority race/ethnic group
ess_data$manydiffgroup_localarea<- as.numeric (with(ess_data, 
                                                    ifelse ((acetalv==3),1,0)))
ess_data$manydiffgroup_localarea[is.na(ess_data$acetalv)  ] <- NA


# People of minority race/ethnic group in current living area ----
# how would you describe the area where you currently live? 2014 only
ess_data$acetalv[ess_data$acetalv > 3] <- NA 
ess_data<-ess_data %>%
  mutate(manyminorities_local= case_when(acetalv==3 ~1,
                                         acetalv==1 |acetalv==2~0))
ess_data$manyminorities_local[is.na(ess_data$acetalv)  ] <- NA



# The ratio between attitudes towards immigrants from the same ethnic group and immigrants from a different ethnic group-----
# imdfetn - Allow immigrants of different race/ethnic group from majority
# imsmetn - Allow immigrants of same race/ethnic group as majority  
# Rs options: 1 Allow many to come and live here --4 Allow none
ess_data$imdfetn[ess_data$imdfetn > 4] <- NA 
ess_data$imsmetn[ess_data$imsmetn > 4] <- NA 
ess_data<-ess_data %>%
  mutate(allowimmig_sameVSdiff_ethnic=imdfetn-imsmetn )

ess_data<-ess_data %>%
  mutate(allowimmig_sameVSdiffethnic_d= case_when(allowimmig_sameVSdiff_ethnic>0 ~1,
                                                      allowimmig_sameVSdiff_ethnic<1~0 ))

ess_data$allowimmig_sameVSdiffethnic_exc1<- as.numeric (with(ess_data, ifelse ((allowimmig_sameVSdiff_ethnic>0 & imsmetn!=1 & imdfetn!=1),1,0)))
# Authoritarian values -----
ess_data$authval_scale <- rowMeans(cbind(ess_data$ipfrule, 
                                         ess_data$impsafe, 
                                         ess_data$ipbhprp, 
                                         ess_data$ipstrgv, 
                                         ess_data$imptrad), na.rm=T)
ess_data$authval_scale[ess_data$authval_scale > 6] <- NA 
ess_data$authoritarian<- as.numeric (with(ess_data, 
                                          ifelse ((authval_scale<2),1,0))) 

ess_data$authoritarian_fac<- factor (with(ess_data, 
                                          ifelse ((authval_scale<2),1,0))) 


# Political system allows people to have a say in what government does -----
# How much would you say the political system in [country] allows people like you to have a say in what the government does?
ess_data$psppsgv[ess_data$psppsgv > 10] <- NA 
ess_data$psppsgva[ess_data$psppsgva > 5] <- NA 

ess_data<-ess_data %>%
  mutate(psppsgv_recoded= case_when(    psppsgv==0 |  psppsgv==1 | psppsgv==2 ~1,
                                        psppsgv==3 |  psppsgv==4  ~2,
                                        psppsgv==5 |  psppsgv==6 ~3,
                                        psppsgv==7 |  psppsgv==8 ~4,
                                        psppsgv==9 |  psppsgv==10  ~5))


ess_data<-ess_data %>%
  mutate(have_a_say_politics= case_when(psppsgv_recoded==1 | psppsgva==1~1,
                                        psppsgv_recoded==2 | psppsgva==2 ~2,
                                        psppsgv_recoded==3 | psppsgva==3 ~3,
                                        psppsgv_recoded==4 | psppsgva==4 ~4,
                                        psppsgv_recoded==5 | psppsgva==5 ~5))

ess_data<-ess_data %>%
  mutate(have_no_voice2= case_when(have_a_say_politics<3 ~1,
                                   have_a_say_politics>2 ~0))

# Political system allows people to have influence on politics -----
# And how much would you say that the political system in [country] allows people like you to have an influence on politics?
ess_data$psppipl[ess_data$psppipl > 10] <- NA 
ess_data$psppipla[ess_data$psppipla > 5] <- NA 
ess_data<-ess_data %>%
  mutate(have_influence_politics = case_when(psppipl==0 |psppipl==1 | psppipl==2 | psppipla==1~1,
                                             psppipl==3 | psppipl==4 | psppipla==2 ~2,
                                             psppipl==5 |  psppipl==6 |psppipla==3 ~3,
                                             psppipl==7 |  psppipl==8 |psppipla==4 ~4,
                                             psppipl==9 |  psppipl==10 |psppipla==5 ~5))



ess_data<-ess_data %>%
  mutate(have_no_influence= case_when(have_influence_politics<3 ~1,
                                      have_influence_politics>2~0))

# Self-perception of social status ------
# There are people who tend to be towards the top of our society and people who tend to be towards the bottom. On this card there is a scale that runs from top to bottom. Where would you place yourself on this scale nowadays?
# 0	Bottom of our society	 
ess_data$plinsoc[ess_data$plinsoc > 10] <- NA 
ess_data<-ess_data %>%
  mutate(lowsocial_status11= case_when(plinsoc==0~1,
                                       plinsoc==1~0.9,
                                       plinsoc==2~0.8,
                                       plinsoc==3~0.7,
                                       plinsoc==4~0.6,
                                       plinsoc==5~0.5,
                                       plinsoc==6~0.4,
                                       plinsoc==7~0.3,
                                       plinsoc==8~0.2,
                                       plinsoc==9~0.1,
                                       plinsoc==10~0))

ess_data<-ess_data %>%
  mutate(low_social_status4= case_when(plinsoc<5~1,
                                       plinsoc>4 ~0))

ess_data<-ess_data %>%
  mutate(low_social_status3= case_when(plinsoc<4~1,
                                       plinsoc>3 ~0))

ess_data<-ess_data %>%
  mutate(low_social_status5= case_when(plinsoc<6~1,
                                       plinsoc>5 ~0))


# Important to get respect from others ----
# It is important to her/him to get respect from others. She/he wants people to do what she/he says.
ess_data$iprspot[ess_data$iprspot > 6] <- NA 
ess_data<-ess_data %>%
  mutate(imp_getrespect= case_when(iprspot<4~1,
                                   iprspot>3 ~0))


# Feel close to the people in local area ----
ess_data$flclpla[ess_data$flclpla > 5] <- NA 
ess_data$distant_people_local<- as.numeric (with(ess_data, ifelse ((flclpla>3),1,0))) 
ess_data$distant_people_local[is.na(ess_data$flclpla)] <- NA




########################################################################### 
## Cultural Accounts - key variables ---                        ###########
###########################################################################  
# Ethno-Cultural Estrangement ------
ess_data$White_Native_img_undermined_cult<- as.numeric (with(ess_data,  ifelse ((born_incountry==1 &  
                                                                                   ethnic_minority==0 &
                                                                                   img_undermined_cult==1),1,0)))
ess_data$White_Native_img_undermined_cult[is.na(ess_data$born_incountry) | 
                                            is.na(ess_data$ethnic_minority) | 
                                            is.na(ess_data$img_undermined_cult)] <- NA

# Intergenerational Backlash ------
ess_data$older55_authoritarian<- as.numeric (with(ess_data, ifelse ((older55==1 &  authoritarian==1),1,0)))

ess_data$older55_authoritarian[is.na(ess_data$older55) | 
                                 is.na(ess_data$authoritarian) ] <- NA

# Rural Resentment ------
ess_data$rural_have_no_voice2<- as.numeric (with(ess_data, ifelse ((Rural_area==1 & have_no_voice2==1),1,0))) 
ess_data$rural_have_no_voice2[is.na(ess_data$Rural_area) | 
                                is.na(ess_data$have_no_voice2) ] <- NA

# Social Status Anxiety ------
ess_data$imp_getrespect2 <- as.numeric (with(ess_data, ifelse ((iprspot<3),1,0))) 
ess_data$imp_getrespect2[is.na(ess_data$iprspot)] <- NA

ess_data$social_status25<- as.numeric (with(ess_data, ifelse ((plinsoc>1 & plinsoc<6),1,0))) 
ess_data$social_status25[is.na(ess_data$plinsoc)] <- NA

ess_data$social_status25_respect2<- as.numeric (with(ess_data, ifelse ((social_status25==1 &
                                                                          imp_getrespect2==1),1,0))) 
ess_data$social_status25_respect2[is.na(ess_data$social_status25) | 
                                    is.na(ess_data$imp_getrespect2) ] <- NA



# Community Disintegration ------
ess_data$noturban<- as.numeric (with(ess_data, ifelse ((domicil!=1),1,0))) 
ess_data$noturban[is.na(ess_data$domicil)] <- NA

ess_data$distant_people_local_noturban<- as.numeric (with(ess_data, ifelse ((distant_people_local==1 & 
                                                                               noturban==1),1,0))) 
ess_data$distant_people_local_noturban[is.na(ess_data$distant_people_local) | is.na(ess_data$noturban)] <- NA




########################################################################### 
## Outcome Variable  ---                        ###########
###########################################################################  

# PopuList typology (vote_choice_PopList_RightPop)   -------------
code_vote_choice_PopList_RightPop <- function(df) {
  df <- df %>%
    mutate(
      vote_choice_PopList_RightPop = case_when(
        # 1.  Switzerland:-------
        # ESS6
        prtvtdch %in% c(2,3,4,5,6,7,8,11,12,13,14,15,16,17,20) ~ 0, # Includes mixed vote
        prtvtdch %in% c(1,9,10)       ~ 1, # Swiss People's Party (1), Federal Democratic Union of Switzerland (9); Ticino League (10); 
        prtvtdch %in% c(19,66)        ~ 2, # Includes blank vote
        prtvtdch %in% c(77,88,99)     ~ 3, # Refusal (77) Don't know (88) No answer (99)
        # ESS7
        prtvtech %in% c(2,3,4,5,6,7,8,11,12,13,14,15,16,18,17, 18, 20) ~ 0, # Mixed vote (18) and Other party (20)
        prtvtech %in% c(1,9,10)       ~ 1,   # Swiss People's Party (1), Federal Democratic Union of Switzerland (9); Ticino League (10); 
        prtvtech %in% c(19,55, 66)  ~ 2,   # Includes blank/no party
        prtvtech %in% c(77,88,99)   ~ 3,   # Refusal (77) Don't know (88) No answer (99)
        #ESS8 
        prtvtfch %in% c(2,3,4,5,6,7,8,11,12,13,14,15,16,18) ~ 0, # Mixed vote (18) and Other party (20)
        prtvtfch %in% c(1,9,10)     ~ 1,  # Swiss People's Party (1), Federal Democratic Union of Switzerland (9); Ticino League (10); 
        prtvtfch %in% c(19,55, 66)  ~ 2, # Includes blank/no party
        prtvtfch %in% c(77,88,99)  ~ 3, # Refusal (77) Don't know (88) No answer (99)
        #ESS9
        prtvtgch %in% c(2,3,4,5,6,7,8,11,12,13,14,15) ~ 0, # Other party (15)
        prtvtgch %in% c(1,9,10)    ~ 1,  # Swiss People's Party (1), Federal Democratic Union of Switzerland (9); Ticino League (10); 
        prtvtgch %in% c(16, 66)    ~ 2,  # Includes blank vote 
        prtvtgch %in% c(77,88,99)  ~ 3,  # Refusal (77) Don't know (88) No answer (99)
        
        # 2.  Germany: ----------
        # Only second votes used
        # ESS6 
        prtvdde2 %in% c(1,2,3,4,5,6,7,8,9)  ~ 0,
        prtvdde2 %in% c(66)       ~ 2,
        prtvdde2 %in% c(77,88,99) ~ 3,
        # ESS7, ESS8, ESS9
        prtvede2 %in% c(1,2,3,4,5,7,8, 9)  ~ 0,
        prtvede2 %in% c(6)      ~ 1, # Alternative for Germany (AFD) (6) 
        prtvede2 %in% c(66)       ~ 2,
        prtvede2 %in% c(77,88,99) ~ 3,
        
        # 3.  Denmark:  ----------
        # ESS6, ESS7:
        prtvtcdk %in% c(1,2,3,4,6,7,8,9,10) ~ 0, # includes other (10)
        prtvtcdk %in% c(5)        ~ 1, #  5. Dansk Folkeparti - Danish peoples party
        prtvtcdk %in% c(66)       ~ 2,
        prtvtcdk %in% c(77,88,99) ~ 3, #
        # ESS9 
        prtvtddk %in% c(1,2,3,4,6,7,8,9,10,11) ~ 0,  # includes other (11)
        prtvtddk %in% c(5)        ~ 1,  # Dansk Folkeparti - Danish peoples party
        prtvtddk %in% c(66)       ~ 2, 
        prtvtddk %in% c(77,88,99) ~ 3,
        # 4.  France:  -----------
        # ESS6, ESS7, ESS8: 
        prtvtcfr %in% c(1,3,4,5,6,7,8, 9,10, 11, 12,13,14) ~ 0, 
        prtvtcfr %in% c(2)        ~ 1, # FN (Front National) (2) 
        prtvtcfr %in% c(15,16,66)   ~ 2, # Includes blank/null
        prtvtcfr %in% c(77,88,99)   ~ 3,
        #ESS9
        prtvtdfr %in% c(1,2,3,4,5,6,7,8,9,12)  ~ 0,
        prtvtdfr %in% c(10, 11)       ~ 1, #FN (Front National) (11); Debout la France (10)
        prtvtdfr %in% c(66)       ~ 2,
        prtvtdfr %in% c(77,88,99) ~ 3,
        
        # 5.  United Kingdom:  -----------
        # ESS6
        prtvtgb  %in% c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22) ~ 0, # includes other (22, 7)
        prtvtgb  %in% c(66)       ~ 2,
        prtvtgb  %in% c(77,88,99) ~ 3,
        # ESS7, ESS8: 
        prtvtbgb %in% c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18) ~ 0, # includes other (18)
        prtvtbgb %in% c(7)        ~ 1, #  Uk Independence Party (7)
        prtvtbgb %in% c(66)       ~ 2,
        prtvtbgb %in% c(77,88,99) ~ 3,
        # ESS9
        prtvtcgb %in% c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,18, 19) ~ 0,
        prtvtcgb %in% c(7)        ~ 1,  #  Uk Independence Party (7)
        prtvtcgb %in% c(66)       ~ 2,
        prtvtcgb %in% c(77,88,99) ~ 3,
        
        # 6.  Hungary:   -----------
        # ESS6:
        prtvtdhu %in% c(1,2,5,6,8,9,10,11,12,13,55) ~ 0, # includes Other (55)
        prtvtdhu %in% c(3,4,7)           ~ 1, # Jobbik (Jobbik Magyarországért Mozgalom) (4); MIÉP (Magyar Igazság És Élet Pártja) (7); Fidesz - KDNP (Fidesz – Magyar Polgári Szövetség Kereszténydemokrata Néppárt) (3)
        prtvtdhu %in% c(66)              ~ 2,
        prtvtdhu %in% c(77,88,99)        ~ 3,
        # ESS7, ESS8:
        prtvtehu %in% c(3,4,5,55)   ~ 0,   # includes Other (55)
        prtvtehu %in% c(1,2)      ~ 1,     # Fidesz - KDNP (Fidesz – Magyar Polgári Szövetség Keresztényd) (1); Jobbik (Jobbik Magyarországért Mozgalom) (2); 
        prtvtehu %in% c(66)       ~ 2,
        prtvtehu %in% c(77,88,99) ~ 3,
        # ESS9:
        prtvtfhu %in% c(1,2,5,6,7,8,9,10,11,55) ~ 0, # includes Other (55)
        prtvtfhu %in% c(3,4)         ~ 1, # Fidesz(3) Jobbik (4); 
        prtvtfhu %in% c(66)          ~ 2,
        prtvtfhu %in% c(77,88,99)    ~ 3,
        # 7.  Italy:  -----------
        # ESS6, ESS8 
        prtvtbit %in%  c(1,2,3,4,5,6,7,11,12,13,14) ~ 0,  
        prtvtbit %in%  c(9, 8, 10)         ~ 1,     # Popolo delle Libertà (8). Lega Nord (9),  Fratelli d'Italia (10)                      
        prtvtbit %in%  c(66)            ~ 2,
        prtvtbit %in%  c(77, 88, 99)    ~ 3,
        # ESS9
        prtvtcit %in% c(1,2,3,4,5,6,7, 11,12,13,14) ~ 0, #Includes other (14)
        prtvtcit %in% c(8,9,10)      ~ 1,      # Popolo delle Libertà (8), Lega Nord (9),  Fratelli d'Italia (10)
        prtvtcit %in% c(66)          ~ 2,
        prtvtcit %in% c(77,88,99)    ~ 3,
        
        # 8.  Netherlands:  -----------
        # ESS6     
        prtvtenl %in% c(1,2,4,5,6,7,8,9,10,11,12,13) ~ 0, # Includes Other (13)
        prtvtenl %in% c(3)           ~ 1,   # PVV (List Wilders)/Party for Freedom  (3)
        prtvtenl %in% c(14,66)         ~ 2, # Includes blank ballot
        prtvtenl %in% c(77,88,99)      ~ 3,
        # ESS7, ESS8:
        prtvtfnl %in% c(1,2,4,5,6,7,8,9,10,11,16) ~ 0, # Includes Other (16)
        prtvtfnl %in% c(3)           ~ 1,   # Party for Freedom (3)
        prtvtfnl %in% c(17,66)         ~ 2, # Includes blank ballots
        prtvtfnl %in% c(77,88,99)      ~ 3,
        # ESS9
        prtvtgnl %in% c(1,2,4,5,6,7,8,9, 10,11,12,13,14,16) ~ 0, # Includes Other (16)
        prtvtgnl %in% c(3)           ~ 1,   # Party for Freedom (3),
        prtvtgnl %in% c(17,66)         ~ 2, # Includes blank ballot (17)
        prtvtgnl %in% c(77,88,99)      ~ 3,
        # 9.  Poland:  -----------
        # ESS6, ESS7:
        prtvtcpl %in% c(1, 2,3,4,5,7,8,9)  ~ 0, # Includes Other (9)
        prtvtcpl %in% c(6)        ~ 1,    # Law and Justice (6)
        prtvtcpl %in% c(66)       ~ 2,
        prtvtcpl %in% c(77,88,99) ~ 3,
        # ESS8, ESS9: 
        prtvtdpl %in% c(1,3,4,5,7,8,9)  ~ 0,  # Includes Other (9)
        prtvtdpl %in% c(2,6)      ~ 1,  # Kukiz'15 (2); Prawo i Sprawiedliwość/aw and Justice (6)
        prtvtdpl %in% c(66)       ~ 2,
        prtvtdpl %in% c(77,88,99) ~ 3,
        
        
        # 10. Sweden:  -----------
        # ESS6, ESS7, ESS8: 
        prtvtbse %in% c(1,2,3,4,5,6,7,8,9,11)   ~ 0, #includes Annat parti (11)
        prtvtbse %in% c(10)       ~ 1, # Sverigedemokraterna (10)
        prtvtbse %in% c(66)       ~ 2,
        prtvtbse %in% c(77,88,99) ~ 3,
        # ESS9
        prtvtcse %in% c(1,2,3,4,5,6,7,8,10)   ~ 0,  #includes Annat parti (10)
        prtvtcse %in% c(9)        ~ 1, #Sverigedemokraterna (9)
        prtvtcse %in% c(66)       ~ 2,
        prtvtcse %in% c(77,88,99) ~ 3,
        
      )
    )
}
ess_data <- code_vote_choice_PopList_RightPop(ess_data)

ess_data <- ess_data[,colSums(is.na(ess_data))<nrow(ess_data)]

# Excluding nonvoters   -------------
ess_data<-subset(ess_data, vote_choice_PopList_RightPop==1 | vote_choice_PopList_RightPop==0)

table(ess_data$cntry2, ess_data$Wave,
      ess_data$vote_choice_PopList_RightPop)
ess_data$vote_RightPop_factor<-factor(ess_data$vote_choice_PopList_RightPop,
                                                   levels=c(0,1), 
                                                   labels = c("Other Parties", "Right Wing Populist Parties"))
table(ess_data$vote_RightPop_factor)


# Radical Right Populist party (an alternative measure of the outcome)   -------------
# Radical Right parties: KDNP in Hungary, KORWiN and UPRKNP in Poland, and NASD in Switzerland.
ess_data$vote_Rad_RightPop <- ess_data$vote_choice_PopList_RightPop
ess_data$vote_Rad_RightPop[ess_data$prtvtfhu==5 
                                              |ess_data$prtvtcpl==1  | 
                                                ess_data$prtvtdpl==1 |  
                                                ess_data$prtvtdch==15 ] <- 1 


# Central Right Parties (an alternative measure of the outcome)  ----------
ess_data$vote_choice_CentralRight <- ess_data$vote_choice_PopList_RightPop

ess_data$vote_choice_CentralRight[ess_data$prtvtdch==4 |
                                  ess_data$prtvtech==4 |
                                  ess_data$prtvtfch==4 |
                                  ess_data$prtvtgch==4 ] <- 2

ess_data$vote_choice_CentralRight[ess_data$prtvede2==2 |
                                    ess_data$prtvtcdk==3 |
                                    ess_data$prtvtddk==3 |
                                    ess_data$prtvtcfr==10 ] <- 2


ess_data$vote_choice_CentralRight[ess_data$prtvtgb==1    |
                                    ess_data$prtvtbgb==1 |
                                    ess_data$prtvtcgb==1  ] <- 2


ess_data$vote_choice_CentralRight[ess_data$prtvtbit==6   |
                                    ess_data$prtvtcit==80   ] <- 2


ess_data$vote_choice_CentralRight[ess_data$prtvtfnl==5   |
                                    ess_data$prtvtfnl==5   ] <- 2


ess_data$vote_choice_CentralRight<-factor(ess_data$vote_choice_CentralRight,
                                                           levels=c(0,1,2), 
                                                           labels = c("Other Parties", 
                                                                      "Right-wing Populist Parties", 
                                                                      "Center-right Parties"))
table(ess_data$vote_choice_CentralRight, ess_data$vote_choice_CentralRight)



#### Economic and cultural indicators (original rural resentment indicator) ----------
ess_data<-ess_data %>%
  rowwise() %>%
  mutate(sum_stories_cult = sum(White_Native_img_undermined_cult, 
                                older55_authoritarian, 
                                rural_have_no_voice2,
                                social_status25_respect2, distant_people_local_noturban, na.rm = TRUE))

ess_data$culturalconcern<- as.numeric (with(ess_data, ifelse ((sum_stories_cult!=0),1,0)))
ess_data$culturalconcern[is.na(ess_data$sum_stories_cult) ] <- NA

ess_data<-ess_data %>%
  rowwise() %>%
  mutate(sum_stories_econ= sum(difficult_subj, 
                               below_2median_income, 
                               production_worker,
                               unemployed_lookingforjob, na.rm = TRUE))
ess_data$economicisequrity<- as.numeric (with(ess_data, ifelse ((sum_stories_econ!=0),1,0)))
ess_data$economicisequrity[is.na(ess_data$sum_stories_econ) ] <- NA
table(ess_data$economicisequrity)


ess_data<-ess_data %>%
  mutate(cult_econ=case_when(economicisequrity==1 & culturalconcern==1 ~"Economic & Cultural concerns",
                             economicisequrity==1 & culturalconcern==0 ~"Economic concern",
                             economicisequrity==0 & culturalconcern==1 ~"Cultural concern",
                             economicisequrity==0 & culturalconcern==0 ~"Neither" ))

ess_data$cult_econ<-factor(ess_data$cult_econ, 
                                    levels=c(       "Neither",
                                                    "Economic concern",
                                                    "Economic & Cultural concerns",
                                                    "Cultural concern" ))


ess_data$economic_con<- as.numeric (with(ess_data, 
                                                  ifelse ((cult_econ=="Economic concern"),1,0)))
ess_data$economic_con[is.na(ess_data$cult_econ) ] <- NA


#### Rural resentment - alternative measure (2012)----------
ess_data$less_turst<- as.numeric (with(ess_data, ifelse ((ess_data$trstprl<5),1,0))) 

ess_data$rural_resentment_trust<- as.numeric (with(ess_data, ifelse ((ess_data$Rural_area==1 & 
                                                                        ess_data$less_turst==1),1,0))) 

ess_data$rural_resentment_trust[is.na(ess_data$less_turst) | 
                                           is.na(ess_data$Rural_area) ] <- NA

ess_data$rural_resentment_trust<-factor(ess_data$rural_resentment_trust)

#### Final data ----------
export(ess_data, "data/ESS_69_newvars.rds")

